import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  // 首页
  {
    path: '/home',
    redirect: '/boder',
    component: () => import('@/views/Home'),
    children: [
      { path: '/boder', component: () => import('@/views/index/Boader') },
      { path: '/medicine', component: () => import('@/views/medicine/Medicine') },
      { path: '/expense', component: () => import('@/views/medicine/Expense') },
      { path: '/user', component: () => import('@/views/admin/UserList') },
      { path: '/doctor', component: () => import('@/views/person/Doctor') },
      { path: '/nurse', component: () => import('@/views/person/Nurse') },
      { path: '/patient', component: () => import('@/views/person/Patient') },
    ]
  },
  //登陆页
  { path: '/login', component: () => import('@/views/Login') },
  // 重定向根路径到登录页面
  { path: '/', redirect: '/login' },
  //注册
  { path: '/register', component: () => import('@/views/Register') },
]

const router = new VueRouter({
  routes
})

// // 挂载路由导航守卫
router.beforeEach((to, from, next) => {
  // to:将要访问的路径
  // from:从哪里访问的路径
  // next:之后要做的任务，是一个函数
  //    next（）放行， next（'/URL'）强制跳转的路径。
  if (to.path == '/login' || to.path == '/register') return next();// 访问路径为登录/注册
  // 获取user
  const user = localStorage.getItem('user')
  console.log(user);
  if (!user) return next('/');// 没登录去登录
  next();
})

export default router
